/// <reference path="../js_intellisense/jquery-1.4.2.js" />
/*
TODO add ParentID field to use in cases such as delete.
TODO factor out getItemHTML() into a method that isn't in the object (with use of template possibly)
*/
var MyList = {
    id: 498349,
    user: 'justinobney',
    lists: jItem({title:'my items'})
};

function jItem(params) {
    var _id = Math.uuid(),
        _parentID,
        _title,
        _items = [],
        _keys = [],
        _find = function (id) {
            var index = _keys.indexOf(id),
                returnItem;
            if (index > -1) {
                returnItem = _items[index];
                return returnItem;
            } else { //enter recursion....
                for (var i in _items) {
                    returnItem = _items[i].getItemByID(id);
                    if (returnItem) {
                        return returnItem;
                    }
                }
            }
            return returnItem;
        };
    return {
        title: params.title || _title,
        complete:  params.complete || false,
        dueDate: params.dueDate || (new Date()),
        notes: params.notes || '',
        parentID: _parentID,
        addItem: function (newItem) {
            var thisItem = jItem(newItem);
            thisItem.parentID = _id;
            _items.push(thisItem);
            _keys.push(thisItem.getID);
        },
        deleteItem: function (itemID) {
            var index = _keys.indexOf(itemID),
                result = '', tempResult = '';
            if (index > -1){
                _items.splice(index, 1);
                _keys.splice(index, 1);
                result = "Item Deleted"
            }  else { //enter recursion....
                for (var i in _items) {
                   tempResult = _items[i].deleteItem(itemID);
                   if(tempResult === 'Item Deleted'){
                       result = tempResult; //Object.create(tempResult);
                       //TODO REMOVE CODE BELOW
                       refreshHTML(); //code in testData.js
                   }
                }
            }
            return (result != '') ? result : 'No item matching ID: ' + itemID + ' found';
        },
        remove:function(){
            var temp = _find(_parentID);
                temp.deleteItem(_id);
        },
        getID: (function () {
            return _id;
        }()),
        getItemByID: _find,
        items: _items,
        getItemHTML: function () {
            var container = jQuery('<div />'),
                //title = jQuery('<div />',{id: _id}),
                ul = jQuery('<ul />'),
                li, div, inner;
            for (var i in _items) {
                li = jQuery('<li />', {id: _items[i].getID, class:"sortable"});
                div = jQuery('<div />').text(_items[i].title);
                li.append(div);
                if (_items[i].items.length) {
                    inner = jQuery(_items[i].getItemHTML());
                    li.append(inner);
                }
                ul.append(li);
            }
            //container.append(title);
            container.append(ul);
            return container.html();
        }
    };
}
